<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>平台组件管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"
            src="../lib/vue/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/global.js"></script>
    <script type="text/javascript" src="../js/store.js"></script>
    <script type="text/javascript" src="../js/cookies.js"></script>
    <script src="../lib/vue/vue.min.js"></script>
    <script src="../lib/vue/axios.min.js"></script>
</head>

<body>
<div class="x-body layui-anim layui-anim-up" id="managerAdd">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="L_userName" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline" style="width: 300px;">
                <input type="text" id="L_userName" name="username" required=""
                       lay-verify="userName"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>非空且唯一
            </div>
        </div>
        <div class="layui-form-item">
            <label for='L_managerFlag' class="layui-form-label">
                <span class="x-red">*</span>角色
            </label>
            <!--3：组件管理员 4:超级管理员-->
            <div class="layui-input-inline">
                <select id="L_managerFlag" name="managerFlag" lay-filter='managerFlag'>
                    <option value=''>--请选择角色--
                    <option value='3'>组件管理员
                    <option value='4'>超级管理员
                </select>
            </div>
        </div>
        <div class="layui-form-item" v-if="managerFlag==='3'">
            <label class="layui-form-label" style="width: 100px">
                <span class="x-red">*</span>负责组件
            </label>
            <div class="layui-input-inline" style="width: 400px;" id="L_componentIds">

            </div>
        </div>
        <div class="layui-form-item" style="padding-top: 100px">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                点击添加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
            $ = layui.jquery
            let form = layui.form
                , layer = layui.layer

            let componentArray;
            //获取组件下拉表
            $.get(_hostUrl + '/cmsComponents', function (res) {
                if (res.responseCode != 200) {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500});
                    return
                }
                componentArray = res.data;
                if (!componentArray || componentArray.length == 0) {
                    layer.msg("还未创建组件信息!")
                } else {
                    for (let i = 0; i < componentArray.length; i++) {
                        $('#L_componentIds').append("<input type='checkbox' lay-filter='componentIds' name='componentIds' value=" + res.data[i].id + " title =" + res.data[i].componentName + ">" + "<br/>")
                    }
                    $('#L_componentIds').val(parseInt(getUrlKey('id')));
                    form.render();
                }
            })
            //自定义验证规则
            form.verify({
                userName: function (value) {
                    if (value.length < 1) {
                        return '用户名不可为空'
                    }
                },
                tel: function (value) {
                    if (value.length < 1) {
                        return '联系方式不可为空'
                    }
                }
            });
            /*  //监控角色下拉框选择事件
              form.on('select(managerFlag)', function (data) {
                  let vm = new Vue({
                      el: '#managerAdd',
                      data() {
                          return {
                              managerFlag: null,
                          }
                      },
                      beforeMount() {
                          this.managerFlag=data.value;
                      }
                  });
              });*/

            let componentValue = "";
            //监控组件列表选择事件
            form.on('checkbox(componentIds)', function () {
                //拼接id
                componentValue = getCheckboxValue('componentIds')
            })
            //监听提交
            form.on('submit(add)', function (data) {
                data.field.componentIds = componentValue;
                $.post(_hostUrl + "/cmsManager", data.field, function (res) {
                    if (res.responseCode == 201) {
                        layer.msg('添加管理员成功', {
                            icon: 1,
                            time: 2000
                        }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name)
                            //关闭当前frame
                            parent.layer.close(index)
                            var table = parent.window.layui.table;
                            table.reload('managerList')
                        });
                    } else {
                        layer.msg(res.responseMsg, {icon: 2, time: 1500})
                    }
                }, 'json')
                return false
            });
        }
    )
</script>
</body>

</html>